package com.robusta.cityuate.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.robusta.cityuate.R;
import com.robusta.cityuate.base.ViewType;
import com.robusta.cityuate.objects.Offering;
import com.robusta.cityuate.utils.TimeUtils;

public class OfferingsListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private ArrayList<Offering> mEntries = new ArrayList<Offering>();
	private Context mContext;
	public ViewType.Type ownershipType;

	public OfferingsListAdapter(Context context,ViewType.Type ownershipType) {
		super();
		this.ownershipType = ownershipType;
		mContext = context;
		mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return mEntries.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntries.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView;
		final Offering offering = mEntries.get(position);
		Log.e("Offering", " "+(offering==null));
		if (convertView == null)
			itemView = mLayoutInflater.inflate(R.layout.offering_list_item,
					parent, false);
		else
			itemView = convertView;

		TextView nameField = (TextView) itemView.findViewById(R.id.offer_name);
		TextView fromField = (TextView) itemView
				.findViewById(R.id.available_at);
		TextView tillField = (TextView) itemView
				.findViewById(R.id.available_till);
		TextView recurringField = (TextView) itemView
				.findViewById(R.id.recurring);
		TextView creditRateField = (TextView) itemView
				.findViewById(R.id.credit_rate);
		TextView minRateField = (TextView) itemView
				.findViewById(R.id.min_rating);
		TextView expiryField = (TextView) itemView
				.findViewById(R.id.expiry);
		TextView statusField = (TextView) itemView
				.findViewById(R.id.status);
		nameField.setText(offering.getName());
		statusField.setText(offering.getStatus());
		fromField.setText(TimeUtils.formatDate(offering.getStartTime(),TimeUtils.DATE_TIME_FORMAT));
		tillField.setText(TimeUtils.formatDate(offering.getEndTime(),TimeUtils.DATE_TIME_FORMAT));
		recurringField
				.setText((offering.getRecurring()==null)?  R.string.none:(offering.getRecurring().equals("w")) ? R.string.weekly
						: ((offering.getRecurring().equals("d")) ? R.string.daily
								: R.string.none));
		creditRateField.setText(offering.getCreditRate() + "");
		minRateField.setText(offering.getMinRating()+"");
		if(offering.getExpiryDate()!=null)
			expiryField.setText(TimeUtils.formatDate(offering.getExpiryDate(),"MMM dd, yyyy"));
		else
			expiryField.setText(R.string.na);

		return itemView;
	}
	
	
	/**
	 * update list of spots after receiving them from service
	 * 
	 * @param entries
	 */
	@SuppressWarnings("unchecked")
	public void upDateEntries(ArrayList<Offering> entries) {
		mEntries = (ArrayList<Offering>) entries.clone();
		notifyDataSetChanged();
	}
	
	
}
